package com.jieshuibao.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.DramaResourceMapper;
import com.jieshuibao.mapper.UserDramaMapper;
import com.jieshuibao.model.db.DramaResource;
import com.jieshuibao.model.db.DramaResourceExample;
import com.jieshuibao.model.db.DramaResourceExample.Criteria;
import com.jieshuibao.model.db.UserDrama;
import com.jieshuibao.model.db.UserDramaExample;
import com.jieshuibao.util.BeanUtils;
import com.jieshuibao.util.ShortUuid;
import com.jieshuibao.vedio.po.DramaResourceDto;
import com.jieshuibao.vedio.po.VediosReq;
import com.jieshuibao.vedio.po.VerifyCodeReq;

@Service
public class DramaResourceService{
	
	@Resource
	private DramaResourceMapper dramaResourceMapper;
	
	@Resource
	private UserDramaMapper userDramaMapper;
	
	@Resource
	private RedisService redisService;
	
	public void addDramaResource(DramaResource dramaResource) {
		dramaResourceMapper.insertSelective(dramaResource);
	}
	
	public void delDramaResourceByVid(String vid) {
		dramaResourceMapper.deleteByPrimaryKey(vid);
	}
	
	public void updateDramaResource(DramaResource dramaResource) {
		dramaResourceMapper.updateByPrimaryKeySelective(dramaResource);
	}
	
	public void updateDramaResourceByExample(DramaResource dramaResource,DramaResourceExample dramaResourceExample){
	    dramaResourceMapper.updateByExampleSelective(dramaResource,dramaResourceExample);
	}
	
	public DramaResource getDramaResourceByVid(String vid) {
		return dramaResourceMapper.selectByPrimaryKey(vid);
	}
	
	public List<DramaResource> getPageDramaResource(DramaResourceExample dramaResourceExample) {
		return dramaResourceMapper.selectPage(dramaResourceExample);
	}
	
	public int countDramaResource(DramaResourceExample dramaResourceExample) {
		return dramaResourceMapper.countByExample(dramaResourceExample);
	}
	
	public List<DramaResource> selectByExample(DramaResourceExample example){
	    return dramaResourceMapper.selectByExample(example);
	}
	//获取视频列表
	public Map<String,Object> getVedioList(VediosReq vedioListReq) {
		String dramaId = vedioListReq.getDramaId();
		DramaResourceExample example = new DramaResourceExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andDramaIdEqualTo(dramaId);
		int pageNum = vedioListReq.getPageNum();
		int pageSize = vedioListReq.getPageSize();
		example.setPageIndex(pageNum);
		example.setPageSize(pageSize);
		int totalCount = dramaResourceMapper.countByExample(example);
		List<DramaResource> dramaResources = dramaResourceMapper.selectPage(example);
//		UserDramaExample userDramaExample = new UserDramaExample();
//		com.jieshuibao.model.db.UserDramaExample.Criteria createCriteria2 = userDramaExample.createCriteria();
//		createCriteria2.andDramaIdEqualTo(dramaId);
//		createCriteria2.andUserIdEqualTo(phoneNumber);
//		List<UserDrama> userDramas = userDramaMapper.selectByExample(userDramaExample);
		List<DramaResourceDto> dtos = new ArrayList<DramaResourceDto>();
		for (DramaResource dramaResource : dramaResources) {
			DramaResourceDto dto = new DramaResourceDto();
			BeanUtils.copyProperties(dto, dramaResource);
			dtos.add(dto);
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("totalCount", totalCount);
		map.put("vedios", dtos);
		return map;
	}
	
	public String getVerifyCode(String phoneNumber,VerifyCodeReq req){
		String dramdId = req.getDramaId();
		String vid = req.getVid();
		UserDramaExample userDramaExample = new UserDramaExample();
		com.jieshuibao.model.db.UserDramaExample.Criteria criteria = userDramaExample.createCriteria();
		criteria.andDramaIdEqualTo(dramdId);
		criteria.andUserIdEqualTo(phoneNumber);
		List<UserDrama> userDramas = userDramaMapper.selectByExample(userDramaExample);
		String verifyCode = "";
		String enable = "";
		DramaResource vedio = dramaResourceMapper.selectByPrimaryKey(vid);
		if(null!=vedio){
			enable = vedio.getEnable();
		}
		if(null!=userDramas&&userDramas.size()>0 || enable.equals("1")){
			//生成播放授权码
			verifyCode = ShortUuid.generateShortUuid()+System.currentTimeMillis();
			redisService.set(verifyCode,vid,120);
		}
		return verifyCode;
	}
}
